package com.yuxl.common.core.utils;

import io.netty.channel.ChannelOption;
import io.netty.handler.timeout.ReadTimeoutHandler;
import io.netty.handler.timeout.WriteTimeoutHandler;
import reactor.netty.http.client.HttpClient;
import reactor.netty.tcp.TcpClient;

import java.time.Duration;
import java.util.concurrent.TimeUnit;

/**
 * @Author : yuxl
 * @create 2025/4/1 12:08
 */
public class HttpUtil {
    public static HttpClient httpClientSetTimeout(Integer channelTimeout,Long readTimeout,Long writeTimeout){
        TcpClient tcpClient = TcpClient.create()
                .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, channelTimeout)
                .doOnConnected(connection ->
                        connection.addHandlerLast(new ReadTimeoutHandler(readTimeout, TimeUnit.MILLISECONDS)) // 读取超时
                                .addHandlerLast(new WriteTimeoutHandler(readTimeout, TimeUnit.MILLISECONDS))); // 写入超时
        // 使用自定义的TCP客户端创建HttpClient
        return HttpClient.from(tcpClient)
                .responseTimeout(Duration.ofMillis(readTimeout)); // 响应超时（与读取超时协同作用）
    }

}
